package com.ellwood.IPGuard.business.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ellwood.IPGuard.business.system.convert.UserConvert;
import com.ellwood.IPGuard.business.system.enums.UserSexEnum;
import com.ellwood.IPGuard.business.system.logic.UmaSysUserLogic;
import com.ellwood.IPGuard.business.system.service.UmsSysUserService;
import com.ellwood.IPGuard.common.core.basic.ServiceFactoryImpl;
import com.ellwood.IPGuard.common.core.constants.Constant;
import com.ellwood.IPGuard.common.core.excel.ExcelFinishCallBack;
import com.ellwood.IPGuard.common.core.result.EllPageResult;
import com.ellwood.IPGuard.common.core.utils.EllExcelUtils;
import com.ellwood.IPGuard.infrastruction.domain.entity.system.UmsSysUserEntity;
import com.ellwood.IPGuard.infrastruction.domain.query.UmsSysUserQuery;
import com.ellwood.IPGuard.infrastruction.domain.vo.system.user.UmsSysUserExcelVO;
import com.ellwood.IPGuard.infrastruction.domain.vo.system.user.UmsSysUserVO;
import com.ellwood.IPGuard.infrastruction.repository.system.UmsSysUserRepository;
import com.fhs.trans.service.impl.TransService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.jta.UserTransactionAdapter;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: IPGuard-boot
 * @PackageName: com.ellwood.IPGuard.business.system.service.impl
 * @ClassName: UmsSysUserServiceImpl
 * @Description: // 用户service接口实现
 * @Author: Ellwood
 * @CreateDate: 2024/9/25 - 下午5:44
 * @Version: V1.0.0
 */
@AllArgsConstructor
@Service
public class UmsSysUserServiceImpl extends ServiceFactoryImpl<UmsSysUserEntity> implements UmsSysUserService {
        
        private final UmsSysUserRepository userRepository;
        
        private final TransService transService;
        
        @Override
        public List<UmsSysUserVO> findAll(UmsSysUserQuery userQuery) {
                // vo->entity放在service业务中直接转换
                List<UmsSysUserEntity> userEntityList = userRepository.findUserAll();
                
                return UserConvert.INSTANCE.convertListEntityTOVo(userEntityList);
        }
        
        @Override
        public UmsSysUserVO getUserById(Long id) {
                UmsSysUserEntity userEntity = userRepository.findUserById(id);
                return UserConvert.INSTANCE.convertEntityTOVo(userEntity);
        }
        
        @Override
        public EllPageResult<UmsSysUserVO> userPage(UmsSysUserQuery userQuery) {
                // 查询参数
                Map<String, Object> pageParams = UmaSysUserLogic.getPageParams(userQuery);
                // 分页查询
                IPage<UmsSysUserEntity> page = getPage(userQuery);
                pageParams.put(Constant.PAGE, page);
                
                
                // 数据列表
                List<UmsSysUserEntity> userList = userRepository.getUserList(pageParams);
                
                return new EllPageResult<>(UserConvert.INSTANCE.convertListEntityTOVo(userList), page.getTotal());
        }
        
        @Override
        public int saveUser(UmsSysUserVO userVO) {
                UmsSysUserEntity userEntity = new UmaSysUserLogic(userRepository).getAddUser(userVO);
                return userRepository.insert(userEntity);
        }
        
        @Override
        public int userUpdate(UmsSysUserVO userVO) {
                UmsSysUserEntity userEntity = new UmaSysUserLogic(userRepository).getUpdateUser(userVO);
                
                // 更新用户角色关系 TODO
                // 更新用户岗位关系 TODO
                // 更新用户缓存权限 TODO
                
                // 更新用户信息
                return userRepository.updateUser(userEntity);
        }
        
        @Override
        public int userRemove(List<Long> idList) {
                // 删除用户角色关系 TODO
                // 删除用户岗位关系 TODO
                
                // 删除用户信息
                return userRepository.userRemove(idList);
        }
        
        @Override
        public int userLogicRemove(Long userId) {
                return userRepository.userLogicRemove(userId);
        }
        
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void userImportExcel(MultipartFile file, String password) {

                EllExcelUtils.readAnalysis(file, UmsSysUserExcelVO.class, new ExcelFinishCallBack<>() {
                        @Override
                        public void doSaveBatch(List<UmsSysUserExcelVO> result) {
                                EllExcelUtils.parseDict(result);
                                List<UmsSysUserEntity> userList = UserConvert.INSTANCE.convertListEVoTOEntity(result);
                                userList.forEach(user->user.setPassword(password));
                                userRepository.insertBatch(userList);
                             /*   for (UmsSysUserEntity user : userList) {
                                        user.setPassword(password);
                                        
                                }   */
                        }
                });
        }
        
        @Override
        @SneakyThrows
        public void userExport() {
                List<UmsSysUserEntity> list=userRepository.getExportUserList();
             
                List<UmsSysUserExcelVO> userExcelVOS = UserConvert.INSTANCE.convertListEntityTOEVo(list);
                
                transService.transBatch(userExcelVOS);
                
                // 写入浏览器打开
                EllExcelUtils.excelExport(UmsSysUserExcelVO.class, "用户信息表",null,userExcelVOS);
        }
        
}
